//
//  LFBSearchResultController.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/12.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBSearchResultController.h"
#import "LFBHomeSearchFlowLayout.h"
#import "LFBSearchResultCell.h"
#import "LFBDetailProductInfoController.h"
#import <YYModel.h>

@interface LFBSearchResultController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong) NSArray *dataArray;

@property(nonatomic,weak) UICollectionView *collectionView;

@property(nonatomic,weak) UIButton *shoppingCartBtn;

@end

static NSString *CELLID = @"CELLID";

@implementation LFBSearchResultController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载数据
    [self loadData];
    
    //设置collectionView
    [self setupCollectionView];
}

- (void)setupCollectionView
{
    //创建flowlayout
    LFBHomeSearchFlowLayout *flowLayout = [LFBHomeSearchFlowLayout new];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.collectionView = collectionView;
    
    [self.view addSubview:collectionView];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerNib:[UINib nibWithNibName:@"LFBSearchResultCell" bundle:nil] forCellWithReuseIdentifier:CELLID];
    
    //创建购物车图标
    UIButton *shoppingCartBtn = [[UIButton alloc] init];
    _shoppingCartBtn = shoppingCartBtn;
    [shoppingCartBtn setImage:[UIImage imageNamed:@"v2_whiteShopBig"] forState:UIControlStateNormal];
    [shoppingCartBtn setBackgroundImage:[UIImage imageNamed:@"v2_shopNoBorder"] forState:UIControlStateNormal];
    shoppingCartBtn.frame = CGRectMake(ScreenWidth / 6 * 5, ScreenHeight / 10 * 9, 60, 60);
    shoppingCartBtn.adjustsImageWhenHighlighted = NO;
    [self.view addSubview:shoppingCartBtn];
}

#pragma - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LFBSearchResultCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    LFBShoppingCartModel *model = self.dataArray[indexPath.item];
    
    cell.model = model;
    
    cell.clickProductView.productCount = [[LFBShoppingCartManager sharedManager] productCountWithShoppingCartModelID:model.product_id];
    
    return cell;
}

//点击item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LFBDetailProductInfoController *detailProductInfoVC = [LFBDetailProductInfoController new];
    
    //获取对应cell的model
    LFBHomeLabelViewCell *cell = (LFBHomeLabelViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //有可能模型数据一样,但是地址不一样
    detailProductInfoVC.shoppingCartModel = [[LFBShoppingCartManager sharedManager] shoppingCartModelWithModelID:cell.model.product_id];
    
    [self.navigationController pushViewController:detailProductInfoVC animated:YES];
}

#pragma - UICollectionViewDelegateFlowLayout -
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, Margin, 0, Margin);
}

- (void)loadData
{
    [SVProgressHUD show];
    
    [LFBNetworkManager searchResponseData:^(id responseObject) {
    
        [SVProgressHUD dismiss];
        
        NSArray<LFBShoppingCartModel *> *arr = [NSArray yy_modelArrayWithClass:[LFBShoppingCartModel class] json:responseObject[@"data"]];
        
        self.dataArray = arr;
        
        //增入到单例模型数组
        [arr enumerateObjectsUsingBlock:^(LFBShoppingCartModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [[LFBShoppingCartManager sharedManager] addShoppingCartProductWithProdunctModel:obj andIsFirstTime:YES];
        }];
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
    }];
}

@end
